//
//  ZBAttributeTextLabel.m
//  ZBTechniqueLibrary
//
//  Created by apple on 2018/9/29.
//  Copyright © 2018年 apple. All rights reserved.
//
/*
 ---------------------
 NSFontAttributeName               设置字体大小和字体的类型 默认12 Helvetica(Neue)
 NSForegroundColorAttributeName    设置字体颜色，默认黑色 UIColor对象
 NSBackgroundColorAttributeName    设置字体所在区域的背景颜色，默认为nil，透明色
 NSLigatureAttributeName           设置连体属性，NSNumber对象 默认0 没有连体
 NSKernAttributeName               设置字符间距， NSNumber浮点型属性 正数间距加大，负数间距缩小
 NSStrikethroughStyleAttributeName 设置删除线，NSNumber对象
 NSStrikethroughColorAttributeName 设置删除线颜色，UIColor对象，默认是黑色
 NSUnderlineStyleAttributeName     设置下划线，NSNumber对象 NSUnderlineStyle枚举值
 NSUnderlineColorAttributeName     设置下划线颜色，UIColor对象，默认是黑色
 NSStrokeWidthAttributeName        设置笔画宽度，NSNumber对象 正数中空 负数填充
 NSStrokeColorAttributeName        设置填充部分颜色，不是指字体颜色，UIColor对象
 NSShadowAttributeName             设置阴影属性，取值为NSShadow对象
 NSTextEffectAttributeName         设置文本特殊效果 NSString对象 只有图版印刷效果可用
 NSBaselineOffsetAttributeName     设置基线偏移量，NSNumber float对象 正数向上偏移，负数向下偏移
 NSObliquenessAttributeName        设置字体倾斜度，NSNumber float对象，正数右倾斜，负数左倾斜
 NSExpansionAttributeName          设置文本横向拉伸属性，NSNumber float对象，正数横向拉伸文本，负数压缩
 NSWritingDirectionAttributeName   设置文字书写方向，从左向右或者右向左
 NSVerticalGlyphFormAttributeName  设置文本排版方向，NSNumber对象。0 横向排版，1 竖向排版
 NSLinkAttributeName               设置文本超链接，点击可以打开指定URL地址
 NSAttachmentAttributeName         设置文本附件，取值为NSTextAttachment对象，一般为图文混排
 NSParagraphStyleAttributeName     设置文本段落排版，为NSParagraphStyle对象
 
 paragraphStyle.lineSpacing = 0.0;//增加行高
 paragraphStyle.headIndent = 0;//头部缩进，相当于左padding
 paragraphStyle.tailIndent = 0;//相当于右padding
 paragraphStyle.lineHeightMultiple = 0;//行间距是多少倍
 paragraphStyle.alignment = NSTextAlignmentLeft;//对齐方式
 paragraphStyle.firstLineHeadIndent = 0;//首行头缩进
 paragraphStyle.paragraphSpacing = 0;//段落后面的间距
 paragraphStyle.paragraphSpacingBefore = 0;//段落之前的间距
 

 ---------------------
 */
#import "ZBAttributeTextLabel.h"


@implementation ZBAttributeTextLabel

/**
 设置不同字体的文字
 */
- (void)zb_setNoramlText:(NSString *)originalText  differentFont:(UIFont *)differentFont  normalFont:(UIFont *)normalFont  differentColor:(UIColor *)differentColor  normalColor:(UIColor *)normalColor range:(NSRange)range
{
    NSMutableAttributedString *attributeString = [self zb_setBaseAttibuteNormalText:originalText noramlFont:normalFont normalColor:normalColor normalBackgroundColor:nil deleteLine:NSUnderlineStyleNone deleteLineColor:nil shadow:nil paragraph:nil];
    
    
    NSInteger normalLength = originalText.length;
    
    if(range.location + range.length > normalLength){
        NSAssert(YES, @"range超过文字范围");
        return;
    }
    
    [attributeString addAttributes:@{NSFontAttributeName : differentFont,NSForegroundColorAttributeName : differentColor} range:range];
    
    self.attributedText = attributeString;
}

/**
 设置价格的Label
 */
- (void)zb_setPriceNoramlText:(NSString *)originalText  differentFont:(UIFont *)differentFont  normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor isLine:(BOOL)isLine lineOffset:(CGFloat)offset
{
    NSMutableAttributedString *attributeString = [self zb_setBaseAttibuteNormalText:originalText noramlFont:normalFont normalColor:normalColor normalBackgroundColor:nil deleteLine:isLine?NSUnderlineStyleSingle:NSUnderlineStyleNone  deleteLineColor:normalColor shadow:nil paragraph:nil];
    
    [attributeString addAttributes:@{NSFontAttributeName : differentFont} range:NSMakeRange(0, 1)];
    
    [attributeString addAttributes:@{NSBaselineOffsetAttributeName :@(offset)} range:NSMakeRange(0, 1)];
    
    self.attributedText = attributeString;
}

/**
 设置标签文字
 */
- (void)zb_setTipText:(NSString *)originalText normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor normalBackgroundColor:(UIColor *)backColor offset:(UIOffset)offest  cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth
{
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.alignment = NSTextAlignmentCenter;
    NSMutableAttributedString *attributeString = [self zb_setBaseAttibuteNormalText:originalText noramlFont:normalFont normalColor:normalColor normalBackgroundColor:backColor deleteLine:NSUnderlineStyleNone  deleteLineColor:nil shadow:nil paragraph:style];
    
    CGSize size =  [attributeString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size;
    
    if(!UIOffsetEqualToOffset(offest, UIOffsetZero)){
        CGFloat width = ceil(size.width) + offest.horizontal * 2;
        CGFloat height  = ceil(size.height) + offest.vertical * 2;
        self.bounds = CGRectMake(0, 0, width, height);
    }
    
    
    if(backColor != nil){
       self.backgroundColor = backColor;
    }
    
    if(borderWidth > 0 || cornerRadius > 0){
        self.layer.cornerRadius = cornerRadius;
        self.layer.borderColor = normalColor.CGColor;
        self.layer.borderWidth = borderWidth;
        self.layer.masksToBounds = YES;
    }
    self.attributedText = attributeString;
}

/**
 设置多行文字
 */
- (void)zb_setMoreNoramlText:(NSString *)originalText  normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor paragraphStyle:(NSMutableParagraphStyle *)paragraphStyle
{
    if(self.numberOfLines != 0){
         self.numberOfLines = 0;
    }
    
    NSMutableAttributedString *attributeString = [self zb_setBaseAttibuteNormalText:originalText noramlFont:normalFont normalColor:normalColor normalBackgroundColor:nil deleteLine:NSUnderlineStyleNone  deleteLineColor:nil shadow:nil paragraph:paragraphStyle];
    
     self.attributedText = attributeString;
    
}

/**
 设置图文文字
 */
- (void)zb_setImageNoramlText:(NSString *)originalText  normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor paragraphStyle:(NSMutableParagraphStyle *)paragraphStyle imageName:(NSString *)imageName imageOffset:(CGFloat)imageoffset
{
    NSMutableAttributedString *attributeString = [self zb_setBaseAttibuteNormalText:originalText noramlFont:normalFont normalColor:normalColor normalBackgroundColor:nil deleteLine:NSUnderlineStyleNone  deleteLineColor:nil shadow:nil paragraph:paragraphStyle];
    
    UIImage *image = [UIImage imageNamed:imageName];
    if(image != nil){
        NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
        attachment.image = image;
        attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
        NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment];
        [attributeString insertAttributedString:imageString atIndex:0];
        
        [attributeString addAttributes:@{NSBaselineOffsetAttributeName :@(imageoffset)} range:NSMakeRange(0, 1)];
    }
     self.attributedText = attributeString;
}

/**
 设置阴影文字
 */
- (void)zb_setShadowNormalText:(NSString *)originalText normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor shadow:(NSShadow *)shadow
{
    NSMutableAttributedString *attributeString = [self zb_setBaseAttibuteNormalText:originalText noramlFont:normalFont normalColor:normalColor normalBackgroundColor:nil deleteLine:NSUnderlineStyleNone deleteLineColor:nil shadow:shadow paragraph:nil];
    
    self.attributedText = attributeString;
}





#pragma mark - 基础赋值
- (NSMutableAttributedString *)zb_setBaseAttibuteNormalText:(NSString *)normalText noramlFont:(UIFont *)normalFont  normalColor:(UIColor *)normalColor normalBackgroundColor:(UIColor *)normalBackgroundColor deleteLine:(NSUnderlineStyle)deleteLineStyle  deleteLineColor:(UIColor *)deleteLineColor shadow:(NSShadow *)shadow   paragraph:(NSParagraphStyle *)paragtaph
{
    if(normalText.length == 0){
        return nil;
    }
    
    
    NSMutableDictionary *originDict = [NSMutableDictionary dictionary];
    
    //1.设置默认字体
    if(normalFont == nil){
        normalFont = [UIFont systemFontOfSize:14.0];
    }
    [originDict setValue:normalFont forKey:NSFontAttributeName];
    
    //2.设置默认颜色
    if(normalColor == nil){
        normalColor = [UIColor blackColor];
    }
    [originDict setValue:normalColor forKey:NSForegroundColorAttributeName];
    
    //3.背景颜色
    if(normalBackgroundColor != nil){
      [originDict setValue:normalBackgroundColor forKey:NSBackgroundColorAttributeName];
    }
    
    //4.删除线
    if(deleteLineStyle != NSUnderlineStyleNone){
        [originDict setValue:@(deleteLineStyle) forKey:NSStrikethroughStyleAttributeName];
    }
    
    //5.删除线颜色
    if(deleteLineColor !=nil){
        [originDict setValue:deleteLineColor forKey:NSStrikethroughColorAttributeName];
    }
    
    
    //6.设置文字阴影
    if(shadow != nil){
         [originDict setValue:shadow forKey:NSShadowAttributeName];
    }
    
    //7.设置段落
    if(paragtaph != nil){
        [originDict setValue:paragtaph forKey:NSParagraphStyleAttributeName];
    }
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:normalText attributes:originDict];
    

    return attributeString;
    
}
@end
